package cn.com.blueInfo.bpm.template.service.impl;

import cn.com.blueInfo.bpm.template.convert.NodeDecisionConvert;
import cn.com.blueInfo.bpm.template.entity.NodeDecision;
import cn.com.blueInfo.bpm.template.repository.NodeDecisionRepository;
import cn.com.blueInfo.bpm.template.request.NodeDecisionDTO;
import cn.com.blueInfo.bpm.template.response.NodeDecisionVO;
import cn.com.blueInfo.bpm.template.service.NodeDecisionService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 节点流转规则表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:30:44
 */
@Log4j2
@Service
public class NodeDecisionServiceImpl implements NodeDecisionService {

    @Autowired
    private NodeDecisionRepository nodeDecisionRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private NodeDecisionConvert nodeDecisionConvert;

    /**
     * 新增节点流转规则
     * @param nodeDecisionDTO 节点流转规则参数
     */
    @Override
    public ResultInfo<Integer> addNodeDecision(NodeDecisionDTO nodeDecisionDTO) {
        NodeDecision nodeDecision = nodeDecisionConvert.dtoToEntity(nodeDecisionDTO);
        nodeDecision.setId(snowflakeIdGenerator.nextId());
        Integer addSize = nodeDecisionRepository.createNodeDecision(nodeDecision);
        return ResultInfo.<Integer>builder().data(addSize).message("节点流转规则成功新增 {} 条").build();
    }

    /**
     * 批量新增节点流转规则
     * @param nodeDecisionDTOList 节点流转规则参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddNodeDecision(List<NodeDecisionDTO> nodeDecisionDTOList) {
        List<NodeDecision> nodeDecisionList = nodeDecisionConvert.dtoToEntity(nodeDecisionDTOList);
        for (NodeDecision nodeDecision : nodeDecisionList) nodeDecision.setId(snowflakeIdGenerator.nextId());
        Integer addSize = nodeDecisionRepository.batchCreateNodeDecision(nodeDecisionList);
        return ResultInfo.<Integer>builder().data(addSize).message("节点流转规则成功新增 {} 条").build();
    }

    /**
     * 删除节点流转规则
     * @param nodeDecisionId 节点流转规则ID
     */
    @Override
    public ResultInfo<Integer> removeNodeDecision(Long nodeDecisionId) {
        Integer removeSize = nodeDecisionRepository.deleteNodeDecision(nodeDecisionId);
        return ResultInfo.<Integer>builder().data(removeSize).message("节点流转规则成功删除 {} 条").build();
    }

    /**
     * 批量删除节点流转规则
     * @param nodeDecisionIdList 节点流转规则ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveNodeDecision(List<Long> nodeDecisionIdList) {
        Integer removeSize = nodeDecisionRepository.batchDeleteNodeDecision(nodeDecisionIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("节点流转规则成功删除 {} 条").build();
    }

    /**
     * 更新部分节点流转规则
     * @param nodeDecisionUpdatePartialDTO 节点流转规则参数
     */
    @Override
    public ResultInfo<Integer> modifyNodeDecisionPartial(
            NodeDecisionDTO.UpdatePartialDTO nodeDecisionUpdatePartialDTO) {
        NodeDecision nodeDecision = nodeDecisionConvert.dtoToEntity(nodeDecisionUpdatePartialDTO);
        Integer modifySize = nodeDecisionRepository.updateNodeDecisionPartial(nodeDecision);
        return ResultInfo.<Integer>builder().data(modifySize).message("节点流转规则成功更新 {} 条").build();
    }

    /**
     * 更新全部节点流转规则
     * @param nodeDecisionDTO 节点流转规则参数
     */
    @Override
    public ResultInfo<Integer> modifyNodeDecision(NodeDecisionDTO nodeDecisionDTO) {
        NodeDecision nodeDecision = nodeDecisionConvert.dtoToEntity(nodeDecisionDTO);
        Integer modifySize = nodeDecisionRepository.updateNodeDecision(nodeDecision);
        return ResultInfo.<Integer>builder().data(modifySize).message("节点流转规则成功更新 {} 条").build();
    }

    /**
     * 查询一条节点流转规则
     * @param nodeDecisionId 节点流转规则ID
     */
    @Override
    public ResultInfo<NodeDecisionVO> queryNodeDecisionById(Long nodeDecisionId) {
        NodeDecision nodeDecision = nodeDecisionRepository.getNodeDecisionById(nodeDecisionId);
        NodeDecisionVO nodeDecisionVO = nodeDecisionConvert.entityToVO(nodeDecision);
        return ResultInfo.<NodeDecisionVO>builder().data(nodeDecisionVO).message("节点流转规则查询成功").build();
    }

    /**
     * 分页查询节点流转规则
     * @param nodeDecisionQueryDTO 节点流转规则参数
     */
    @Override
    public ResultInfo<Page<NodeDecisionVO>> queryNodeDecisionPage(NodeDecisionDTO.QueryDTO nodeDecisionQueryDTO) {
        Page<NodeDecision> nodeDecisionPage = nodeDecisionRepository.getNodeDecisionPage(nodeDecisionQueryDTO);
        Page<NodeDecisionVO> resultData = nodeDecisionConvert.convertPage(nodeDecisionPage);
        return ResultInfo.<Page<NodeDecisionVO>>builder()
                .data(resultData)
                .message("节点流转规则分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 节点流转规则 数量
     * @param nodeDecisionQueryDTO 节点流转规则参数
     */
    @Override
    public ResultInfo<Long> queryNodeDecisionCount(NodeDecisionDTO.QueryDTO nodeDecisionQueryDTO) {
        Long resultCount = nodeDecisionRepository.getNodeDecisionCount(nodeDecisionQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("节点流转规则数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 节点流转规则
     * @param nodeDecisionQueryDTO 节点流转规则参数
     */
    @Override
    public ResultInfo<List<NodeDecisionVO>> queryNodeDecisionList(NodeDecisionDTO.QueryDTO nodeDecisionQueryDTO) {
        List<NodeDecision> nodeDecisionList = nodeDecisionRepository.getNodeDecisionList(nodeDecisionQueryDTO);
        List<NodeDecisionVO> resultData = nodeDecisionConvert.entityListToVoList(nodeDecisionList);
        return ResultInfo.<List<NodeDecisionVO>>builder()
                .data(resultData)
                .message("节点流转规则分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
